<?php


namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\ShopcategoryModel;

class Shopcategory extends AdminBase
{
    protected ShopcategoryModel $model;

    /**
     * 初始化
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->model = new ShopcategoryModel();
    }

    /**
     * 商品类别列表
     */
    public function index()
    {
        $param = $this->request->param();
        $model = $this->model;
        //构造搜索条件
        
        if( $param['start_date']!=''&&$param['end_date']!='' ) {
            $start_date = $param['start_date'].' 00:00:00';
            $end_date = $param['end_date'].' 23:59:59';
            $model = $model->whereBetweenTime('create_time',$start_date,$end_date);
        }
        $list = $model->order('weigh asc')
            ->paginate([
                'query' => $param,
                'list_rows'=>$this->request->param('limit',20)
            ]);
        return view('', compact('list'));
    }

    /**
     * 添加商品类别
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = $this->model->save($param);
            if( $result ) {
                xn_add_admin_log('添加商品类别');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return view('form');
    }

    /**
     * 编辑商品类别
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = $this->model->update($param);
            if( $result ) {
                xn_add_admin_log('编辑商品类别');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $_data = $this->model->find($id);
        $data = $_data->toArray();
        return view('form',compact('data'));
    }

    /**
     * 删除商品类别
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $this->model->destroy($id);
        xn_add_admin_log('删除商品类别');
        $this->success('删除成功');
    }
}